home *** CD-ROM | disk | FTP | other *** search
- /*
- Printing.c
-
- This file contains the printing routines for "all shapes".
-
- ©1992-1994 Apple Computer, Inc.
- All rights reserved.
-
- 3/22/94 - dmh - Updated for B4.
- 3/24/94 - dmh - general cleanup/debugging.
- 5/3/94 - dmh - more cleanup/debugging.
- 8/24/94 - dmh - universalized.
- */
-
- #include <GXPrinting.h>
- #include "GXExceptions.h"
- #include "graphics shell.h"
- #include "memory.h"
-
-
- /*------ SetUpEditMenuRec ------------------------------------------------------------------------------------*/
- //
- // This routine sets up an gxEditMenuRecord which references our edit menu. This structure
- // is used by the GXJobDefaultFormatDialog and GXJobPrintDialog calls to allow cut, copy, & paste
- // operations from the dialogs.
- //
- void SetUpEditMenuRec(gxEditMenuRecord *edMenuRec)
- {
-
- edMenuRec->editMenuID = mEdit;
- edMenuRec->cutItem = iCut;
- edMenuRec->copyItem = iCopy;
- edMenuRec->pasteItem = iPaste;
- edMenuRec->clearItem = iClear;
- edMenuRec->undoItem = iUndo;
- }
-
-
- /*------ DoFormat ------------------------------------------------------------------------------------*/
- //
- // This routine performs GX's equivalent of the Page Setup (PrStlDialog) call of
- // the old printing architecture.
- //
- OSErr DoFormat(WindowPtr wind, gxDialogResult *result)
- {
- OSErr err;
- gxEditMenuRecord edMenuRec;
- gxJob docJob;
-
- // If we have a non-nil WindowPtr, set up our edit menu record and handle the job
- // format dialog. Remember to check for errors.
-
- if (wind)
- {
- docJob = GetDocJob(wind);
- SetUpEditMenuRec(&edMenuRec);
- *result = GXJobDefaultFormatDialog(docJob, &edMenuRec);
- }
-
- return GXGetJobError(docJob);
- }
-
-
- /*------ DoPrinting ----------------------------------------------------------------------------------*/
- //
- // This routine performs GX's equivalent of the Print gxJob Dialog (PrJobDialog) call of
- // the old printing architecture, and then prints the document if the user wants to.
- //
- OSErr DoPrinting(WindowPtr wind)
- {
- OSErr err = noErr;
- gxDialogResult result;
- gxEditMenuRecord edMenuRec;
- gxJob docJob;
-
- // If we have a non-nil WindowPtr, set up our edit menu record and handle the print
- // job dialog. Remember to check for errors. If no errors occur, and the user clicks
- // ok, print the window's document.
-
- if (wind)
- {
- docJob = GetDocJob(wind);
-
- SetUpEditMenuRec(&edMenuRec);
- result = GXJobPrintDialog(docJob, &edMenuRec);
- err = GXGetJobError(docJob);
-
- if ((result == gxOKSelected) && !(err))
- err = DoPrintLoop(wind);
- }
-
- return err;
- }
-
-
-
- /*------ DoPrintLoop ----------------------------------------------------------------------------------*/
- //
- // This routine prints one copy of the window's document using whatever job and format
- // is currently attached to it. (If the window wwere just created and then the user
- // selected Print One Copy w/o first selecting Page Setup…, the system default job and
- // format are stored with this document.
- //
- OSErr DoPrintLoop(WindowPtr wind)
- {
- OSErr err = noErr;
- gxJob docJob;
- Str255 title;
-
- // If we have a non-nil WindowPtr, start the print job, print a page and then finish
- // the job. Since we have just a one page document, we don't bother counting pages.
- // Remember to check those errors!
-
- if (wind)
- {
- docJob = GetDocJob(wind);
- GetWTitle(wind, title);
-
- GXStartJob(docJob, title, 1);
- err = GXGetJobError(docJob);
-
- if (!err)
- {
- GXPrintPage(docJob, 1, GXGetJobFormat(docJob, 1), GetDocShape(wind));
- err = GXGetJobError(docJob);
- }
-
- GXFinishJob(docJob);
- if (!err) err = GXGetJobError(docJob);
- }
- return err;
- }
-
-
- /*******************************************************************
- DoPrintOneCopy sets up our job collection items for printing
- one copy of a document, and then prints the document.
-
- ********************************************************************/
-
- OSErr DoPrintOneCopy(WindowPtr wind)
- {
- OSErr err;
- Collection jobCollection;
- gxCopiesInfo copiesInfo;
- gxFileDestinationInfo destInfo;
- gxPageRangeInfo pageRangeInfo;
- Ptr oldCopiesInfo = nil, oldPageRangeInfo = nil, oldDestInfo = nil;
- long oldCopiesSize, oldPageRangeInfoSize, oldDestInfoSize;
- TH_Doc doc = (TH_Doc) GetWRefCon(wind);
-
- /* Get the job collection and set it up to print one copy… */
-
- jobCollection = GXGetJobCollection(GetDocJob(wind));
-
-
- /* Set number of copies to 1. */
-
- copiesInfo.copies = 1;
- err = MyReplaceCollectionItem(&copiesInfo, sizeof(gxCopiesInfo),
- gxCopiesTag, gxPrintingTagID,
- jobCollection, &oldCopiesInfo, &oldCopiesSize);
- nrequire(err, ReplaceCopies_error);
-
-
- /* Set page range to "all". */
-
- pageRangeInfo.simpleRange.optionChosen = gxDefaultPageRange;
- pageRangeInfo.minFromPage = 1;
- pageRangeInfo.simpleRange.fromPage = 1; // This app only handles one page docs.
- pageRangeInfo.maxToPage = 1;
- pageRangeInfo.simpleRange.toPage = 1;
- pageRangeInfo.simpleRange.printAll = true;
- err = MyReplaceCollectionItem(&pageRangeInfo, sizeof(gxPageRangeInfo),
- gxPageRangeTag, gxPrintingTagID,
- jobCollection, &oldPageRangeInfo, &oldPageRangeInfoSize);
- nrequire(err, ReplacePageRange_error);
-
-
- /* Set destination to "printer". */
-
- destInfo.toFile = false;
- err = MyReplaceCollectionItem(&destInfo, sizeof(gxFileDestinationInfo),
- gxFileDestinationTag, gxPrintingTagID,
- jobCollection, &oldDestInfo, &oldDestInfoSize);
- nrequire(err, ReplaceDestination_error);
-
-
- /* Print one copy of our document. */
-
- err = DoPrintLoop(wind);
-
-
- /* Restore original number of copies, page range, and output
- destination in case anybody uses that info. */
-
- ReplaceCopies_error:
- MyReplaceCollectionItem(oldCopiesInfo, oldCopiesSize,
- gxCopiesTag, gxPrintingTagID,
- jobCollection, nil, nil);
-
- ReplacePageRange_error:
- MyReplaceCollectionItem(oldPageRangeInfo, oldPageRangeInfoSize,
- gxPageRangeTag, gxPrintingTagID,
- jobCollection, nil, nil);
-
- ReplaceDestination_error:
- MyReplaceCollectionItem(oldDestInfo, oldDestInfoSize,
- gxFileDestinationTag, gxPrintingTagID,
- jobCollection, nil, nil);
-
-
- /* Dispose of the pointers that MyReplaceCollectionItem created. */
-
- if (oldCopiesInfo)
- DisposePtr(oldCopiesInfo);
-
- if (oldPageRangeInfo)
- DisposePtr(oldPageRangeInfo);
-
- if (oldDestInfo)
- DisposePtr(oldDestInfo);
-
- return err;
- }
-
-
-
- /*******************************************************************
- MyReplaceCollectionItem is a generic routine that replaces a
- collection item with the passed data. If the oldData parameter
- is not nil, this routine creates a pointer and returns the data
- that's being replaced in it. (If the item doesn't already exist,
- then a copy of the newData is returned instead.)
-
- ********************************************************************/
-
- OSErr MyReplaceCollectionItem(void *newData, long collectSize,
- OSType collectType, long collectID,
- Collection whichCollection,
- Ptr *oldData, long *oldDataSize)
- {
- OSErr err = noErr;
- long index;
-
- /*
- If we're supposed to return the old data, get it.
- If there is no old data, return a copy of the new data.
- */
-
- if (oldData)
- {
- err = GetCollectionItemInfo(whichCollection,
- collectType,
- collectID,
- dontWantIndex,
- oldDataSize,
- dontWantAttributes);
-
- if (err)
- {
- *oldDataSize = collectSize;
- *oldData = NewPtrSys(*oldDataSize);
- if (!(err = MemError()))
- BlockMove(newData, *oldData, collectSize);
- }
- else
- {
- *oldData = NewPtrSys(*oldDataSize);
- if (!(err = MemError()))
- err = GetCollectionItem(whichCollection,
- collectType,
- collectID,
- dontWantSize,
- *oldData);
- }
- }
-
- nrequire(err, CouldNotSetOldData);
-
-
- // If we're adding a new collection item, do so. Otherwise, get the
- // existing item's index and replace the old collection item.
-
- err = AddCollectionItem(whichCollection,
- collectType,
- collectID,
- collectSize,
- newData);
-
- if (err == collectionItemLockedErr)
- {
- err = GetCollectionItemInfo(whichCollection,
- collectType,
- collectID,
- &index,
- dontWantSize,
- dontWantAttributes);
- if (!err)
- err = ReplaceIndexedCollectionItem(whichCollection,
- index,
- collectSize,
- newData);
- }
-
- CouldNotSetOldData:
- return err;
- }
-